package com.cz.nongyetongv5.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.activity.EntDetailActivity;
import com.cz.nongyetongv5.activity.MapViewActivity;


public class EntInfoFamilyFragment extends Fragment {
    private TextView tv_type;
    private TextView tv_business_license;
    private TextView tv_addr;
    private TextView tv_scale;
    private TextView tv_transportation;
    private TextView tv_transportation_link;
    private TextView tv_transportation_type;


    private RelativeLayout rl_transportation_link;
    private RelativeLayout rl_transportation_type;


    private View view;
    private ImageView iv_map;

    public static EntInfoFamilyFragment newInstance() {
        EntInfoFamilyFragment fragment = new EntInfoFamilyFragment();
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_ent_info_family, null);
        init();
        return view;
    }


    public void init() {
        tv_type = view.findViewById(R.id.tv_type);
        tv_business_license = view.findViewById(R.id.tv_business_license);
        tv_addr = view.findViewById(R.id.tv_addr);
        tv_scale = view.findViewById(R.id.tv_scale);
        tv_transportation = view.findViewById(R.id.tv_transportation);
        tv_transportation_link = view.findViewById(R.id.tv_transportation_link);
        tv_transportation_type = view.findViewById(R.id.tv_transportation_type);


        rl_transportation_link = view.findViewById(R.id.rl_transportation_link);
        rl_transportation_type = view.findViewById(R.id.rl_transportation_type);


        if (((EntDetailActivity) getActivity()).getEntBean().getCategory_name() != null) {
            tv_type.setText(((EntDetailActivity) getActivity()).getEntBean().getCategory_name());
        }

        if (((EntDetailActivity) getActivity()).getEntBean().getBusiness_license() != null) {
            tv_business_license.setText(((EntDetailActivity) getActivity()).getEntBean().getBusiness_license());
        }
        if (((EntDetailActivity) getActivity()).getEntBean().getAddress() != null) {
            tv_addr.setText(((EntDetailActivity) getActivity()).getEntBean().getAddress());
        }
        if (((EntDetailActivity) getActivity()).getEntBean().getScale_amount() != null) {
            tv_scale.setText(((EntDetailActivity) getActivity()).getEntBean().getScale_amount() +
                    ((EntDetailActivity) getActivity()).getEntBean().getScale_group_name());
        }

        if (((EntDetailActivity) getActivity()).getEntBean().getIs_transportation().equals("0")) {
            tv_transportation.setText("否");
            rl_transportation_link.setVisibility(View.GONE);
            rl_transportation_type.setVisibility(View.GONE);
        } else {
            tv_transportation.setText("是");
            rl_transportation_link.setVisibility(View.VISIBLE);
            rl_transportation_type.setVisibility(View.VISIBLE);
            tv_transportation_link.setText(((EntDetailActivity) getActivity()).getEntBean().getTrans_tache_name());
            tv_transportation_type.setText(((EntDetailActivity) getActivity()).getEntBean().getTrans_category_name());
        }

        iv_map = view.findViewById(R.id.iv_map);
        iv_map.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (((EntDetailActivity) getActivity()).getEntBean().getLatitude() == null || ((EntDetailActivity) getActivity()).getEntBean().getLatitude() == 0f) {
                    Toast.makeText(getActivity(), "暂无坐标信息", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(getActivity(), MapViewActivity.class);
                    intent.putExtra("latitude", ((EntDetailActivity) getActivity()).getEntBean().getLatitude());
                    intent.putExtra("longitude", ((EntDetailActivity) getActivity()).getEntBean().getLongitude());
                    startActivity(intent);
                }
            }
        });
    }
}